package net.sahv.bdyz.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.ShowSynchronization;
import net.sahv.bdyz.model.ShowSynchronizationPic;
import net.sahv.bdyz.service.LanguageService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.PatternService;
import net.sahv.bdyz.service.ShowSynchronizationPicService;
import net.sahv.bdyz.service.ShowSynchronizationService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

/**
 * @author 作者 : 任凯伟
 * @date 创建时间：2016年8月3日 下午1:56:32
 * @version 1.0
 * @see 秀场同步
 */
@Controller
@RequestMapping("api/show")
public class ShowController {
	
	@Autowired
	private ShowSynchronizationService showSynchronizationService;// 秀场
	@Autowired
	private ShowSynchronizationPicService showSynchronizationPicService;// 秀场图片
	@Autowired
	private PatternService patternService;// 花型
	@Autowired
	private MemberService memberService;// 会员
	@Autowired
	private LanguageService languageService;// 语言

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月3日 下午1:59:08
	 * @version 1.0
	 * @see 秀场同步主页列表
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value="/showFieldList",method=RequestMethod.GET)
	public String showFieldList(Model model) {
		String language = languageService.getCurrentLanguage();
		List<ShowSynchronization> showList = showSynchronizationService.selectAll();// 全部秀场同步
		if ("en".equals(language)) {
			for (ShowSynchronization ss : showList) {
				ss.setName(ss.getEnName());
			}
		}
		model.addAttribute("showList", showList);
		return "phone/show/show";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月3日 下午2:33:35
	 * @version 1.0
	 * @see 秀场追踪
	 * @param
	 * @parameter sId 秀场id
	 * @since
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	@RequestMapping("/details")
	public String showFollow(Integer sId, PageInfo pageInfo, Model model) throws FileNotFoundException, IOException {
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		String imgParent = mapUrls.get("targetUrl.img");
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		pageInfo.setPageSize(20);
		if (pageInfo.getPage() < 2 || pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		String where = " p.P_IS_DELETE=0 and p.P_SEND_STATE=1 and p.P_BUYOUT_STATE=0";
		String orderStr = " order by p.P_SEND_TIME desc";
		if (sId != null) {
			Member member = memberService.getCurrent();
			where += " and p.SS_ID=" + sId;
			int mId = 0;
			if (member != null) {
				mId = member.getmId();
			}
			ShowSynchronization show = showSynchronizationService.selectById(sId);// 秀场信息
			List<ShowSynchronizationPic> showPicList = showSynchronizationPicService.getShowPicListByShowId(sId);// 秀场图片
			// 花型列表 分页
			List<Map<String, Object>> pList = patternService.getByPage(pageInfo, where, orderStr, mId);
			if (pList.size() > 0) {
				pageInfo.setCount(Integer.parseInt(pList.get(0).get("myCount").toString()));
				pageInfo.setTotalPage((int) Math
						.ceil(Integer.parseInt(pList.get(0).get("myCount").toString()) / pageInfo.getPageSize()));
				for (Map<String, Object> pListOne : pList) {
					String pImg = (String) pListOne.get("pImg");
					pListOne.remove("pImg");
					pListOne.put("pImg", imgParent + pImg);
				}
			}
			model.addAttribute("show", show);
			model.addAttribute("showPicList", showPicList);
			model.addAttribute("pList", pList);
			model.addAttribute("page", pageInfo);
			model.addAttribute("member", member);
		}
		return "phone/show/showDetail";
	}
}
 